package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.bo.PtSysExpClassBo;
import com.ruoyi.system.domain.vo.PtSysExpClassVo;
import com.ruoyi.system.domain.PtSysExpClass;
import com.ruoyi.system.mapper.PtSysExpClassMapper;
import com.ruoyi.system.service.IPtSysExpClassService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 班级实验对应Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-26
 */
@Service
public class PtSysExpClassServiceImpl extends ServicePlusImpl<PtSysExpClassMapper, PtSysExpClass, PtSysExpClassVo> implements IPtSysExpClassService {

    @Override
    public PtSysExpClassVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PtSysExpClassVo> queryPageList(PtSysExpClassBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PtSysExpClass> lqw = buildQueryWrapper(bo);
        Page<PtSysExpClassVo> result = pageVo(pageQuery.build(), lqw);

        return TableDataInfo.build(result);
    }

    @Override
    public List<PtSysExpClassVo> queryList(PtSysExpClassBo bo) {
        LambdaQueryWrapper<PtSysExpClass> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<PtSysExpClass> buildQueryWrapper(PtSysExpClassBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PtSysExpClass> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getClassId() != null, PtSysExpClass::getClassId, bo.getClassId());
        lqw.eq(bo.getExpId() != null, PtSysExpClass::getExpId, bo.getExpId());
        lqw.eq(bo.getEndTime() != null, PtSysExpClass::getEndTime, bo.getEndTime());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PtSysExpClassBo bo) {
        PtSysExpClass add = BeanUtil.toBean(bo, PtSysExpClass.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(PtSysExpClassBo bo) {
        PtSysExpClass update = BeanUtil.toBean(bo, PtSysExpClass.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PtSysExpClass entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
